package com.com.knowledgepaymentmanagementsystem.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.com.knowledgepaymentmanagementsystem.mapper.user.UserMapper;
import com.com.knowledgepaymentmanagementsystem.pojo.courseshop.CourseShop;
import com.com.knowledgepaymentmanagementsystem.pojo.login.LoginBean;
import com.com.knowledgepaymentmanagementsystem.pojo.user.User;
import com.com.knowledgepaymentmanagementsystem.service.user.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//声明业务层
@Service
public class UserServiceImpl implements UserService {
    // 注入 UserMapper 接口
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findUserList(User user) {

        //设置分页参数
        PageHelper.startPage(user.getPage(),user.getPageSize());
        //执行查询
        List<User> userList = userMapper.findUserList(user);
        //因为在pagehelper之后执行的查询语句本身是page类型的，它还包含了分页信息
        // 转换为普通ArrayList，移除分页信息
        List<User> resultList = new ArrayList<>(userList);
        return resultList;
    }

    @Override
    public void delUserById(Integer id) {
        userMapper.delUserById(id);
    }

    @Override
    public String addUser(User user) {

        user.setCreateTime(LocalDateTime.now());
        //判断用户名是否被使用
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("user_info",user.getUserInfo());
        User textData = userMapper.selectOne(query);
        if(null == textData) {
            //账号使用用户名
            user.setAccount(user.getUserInfo());
            //密码默认为111111
            user.setPassword("666666");
            userMapper.insert(user);
//            //添加用户角色中间表
//            userMapper.addUserAndRole(text);


        }else {
            //手机号存在不可用,返回失败信息，让换手机号
//            message = "手机号码已被使用，请更换";
            throw new RuntimeException();
//            return "用户名已被使用，请更换";
        }
        //手机号存在,返回成功信息
        return "添加用户成功，登录账号为："+ user.getUserInfo() + "默认密码为：666666";

    }

    @Override
    public void updataUser(User user) {
        userMapper.updataUser(user);
    }

    @Override
    public HashMap<String, Object> login(LoginBean loginBean) {
        //精确提示信息
        String msg = "";
        //1.创建map集合
        HashMap<String, Object> map = new HashMap<>();

        //2.根据用户输入的账号查询：账号从loginBean参数中查询
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("account",loginBean.getAccount());
        User text = userMapper.selectOne(query); //text 来自数据库的用户数据
        //3.判断 user
        if(null == text){
            msg = "账号不存在";
        }
        else {
//            4.验证账号状态是否可以正常登录
            if(text.getUserState() == 1){
                //正常，验证密码
                //4.验证密码
                if(loginBean.getPassword().equals(text.getPassword())){
                    msg = "登录成功";
                    //存储用户信息
                    map.put("user",text);
                }
                else {
                    msg = "密码错误";
                }
            }else {
                //账号被禁用
                msg="账号已被禁用，联系管理员";
            }

        }
        //存储到map集合中
        map.put("msg",msg);
        return map;
    }

    @Override
    public void updateShopUser(CourseShop courseShop) {
        String userInfo = courseShop.getUserInfo();
        User user = userMapper.selectUser(userInfo);
        user.setCostNum(user.getCostNum() + 1);
        Double price =  user.getCostMoney() + courseShop.getPrice();
        user.setCostMoney(price);
        userMapper.updateById(user);
    }
}
